/**
 * @title HHEnterAction.java
 * @description 	用户进入管理动作文件
 * @create 			2012-7-18 下午08:00:17 By xjiujiu
 * @package 		apps.admin.action
 * @copyright 		Copyright (c) 2011-2012 http://xyrj.hhtc.edu.cn.com. All right reserved
 * @version         $Id$
 * XYRJ-Java-Project
 */
package apps.admin.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import system.config.HHConfig;
import system.pojo.HHUserPojo;

import apps.admin.form.HHLoginForm;
import apps.admin.model.HHUserModel;

import com.xyrj.exception.HHValidateException;
import com.xyrj.image.HHVerificationCodeUtils;
import com.xyrj.mvc.HHAction;
import com.xyrj.mvc.HHView;
import com.xyrj.utils.HHVerify;

/**
 * 用户进入管理动作
 * 
 * 用户登陆、退出后台动作集合 
 * @ClassName: HHIndexAction 
 * @Description:
 * @author maoyun0903(maoyun19900818@163.com);
 * @date 2012-10-8 下午9:01:44 
 * @version V1.0
 */
public class HHIndexAction extends HHAction
{
	
	/** 
     * 构造函数
     * 
     * 初始化类成员
     * 
     * @param HttpServletRequest request 当前用户的请求对象
     * @param HttpServletResponse response 当前Http反馈对象
     */
    public HHIndexAction(HttpServletRequest request,
            HttpServletResponse response)
    {
	    super(request, response);
	    
	    this._pojo 	= new HHUserPojo();
    }
   
    /**
     * 主方法
     *  
     * 当用户没有指定所要执行的动作时，默认执行此方法 
     * 
     * @title  index
     * @return void
     */
    public void index()
    {
    	if(false == HHIndexAction.isLogined(this._view)) {
    		this._view.setRequestAttr("hintInfo", "请输入您的用户名跟密码。");
    		this._view.display("admin/login.jsp");
    	} else {
    		this._view.redirect(HHConfig.SITE_URL + "/index.jsp/admin/admin");
    	}
    }
   
    /**
     * 用户登陆动作
     *  
     * 验证用户登陆信息 
     * 
     * @title  login
     * @return void
     */
    @SuppressWarnings("rawtypes")
    public void login()
    {
    	try {
	    	HHLoginForm loginForm 	= new HHLoginForm();
	    	this._pojo.setField("user_name", this._view.getParameter("user_name"));
	    	this._pojo.setField("password", this._view.getParameter("password"));
    		loginForm.validate(this._pojo);
    		//loginForm.valicode(this._view.getRequestAttr("validate"),this._view.getSessionAttr("valicode"));
    	} catch(HHValidateException ex) {
    		this._view.setRequestAttr("hintInfo", ex.getMessage());
    		this._view.display("admin/login.jsp");
    	}
    	HHUserModel user 	= new HHUserModel(this._pojo);
    	Map userInfo 		= user.checkLogin(this._pojo);
    	if(null == userInfo) {
    		this._view.setRequestAttr("hintInfo", "用户名或密码错误！");
    		this._view.display("admin/login.jsp");
    		return;
    	}
    	this._setUserLoginInfo(userInfo);
		this._view.redirect(HHConfig.SITE_URL + "/index.jsp/admin/admin");
	}
   
    /**
     * 设置用户登陆信息
     *  
     * @description
     * 
     * @title  _setUserLoginInfo
     * @return void
     */
    @SuppressWarnings("rawtypes")
    protected void _setUserLoginInfo(Map userInfo)
    {
    	this._view.setSessionAttr("xyrj_user_id", userInfo.get("user_id"));
    	this._view.setSessionAttr("xyrj_user_name", userInfo.get("user_name"));
    }
    
    /**
     * 用户登出动作
     *  
     * @description
     * 
     * @title  logout
     * @return void
     */
    public void logout()
    {
    	this._view.setSessionAttr("xyrj_user_id", null);
    	this._view.setSessionAttr("xyrj_user_name", null);
    	this._view.alertAndRefresh("退出成功，正在跳转中...",
			HHConfig.SITE_URL+"/index.jsp/admin", 2, "document.write");
    }
	   
    /**
     * 验证用户是否登陆
     *  
     * 检测当前的请求里是否含有用户ID，用户名的Session属性 
     * 
     * @title  isLogined
     * @param  HttpServletRequest request 用户的当前请求
     * @return boolean
     */
    public static boolean isLogined(HHView view) 
    {
    	if(null == view.getSessionAttr("xyrj_user_id")) {
    		return false;
    	}
    	if(null == view.getSessionAttr("xyrj_user_name")) {
    		return false;
    	}
    	
    	return true;
    }

}
